package com.llbnk.springcoding.loader;

import com.alibaba.excel.util.StringUtils;
import com.llbnk.springcoding.pojo.TestInform;
import org.json.JSONObject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

@Component
public class TestLoader implements InitializingBean {

    private Map<String, LoaderFunction> workersMap = new HashMap<>();

    //获取加载器名称
    @Override
    public void afterPropertiesSet() throws Exception {
        for (Field field : LoaderName.class.getDeclaredFields()) {
            if (Modifier.isPublic(field.getModifiers())
                    && Modifier.isStatic(field.getModifiers())
                    && Modifier.isFinal(field.getModifiers())
                    && String.class.equals(field.getType())) {
                //这步骤的意思是拿出数据是static的name，因为static不是对象所以是void ==》null
                String workerName = (String) field.get(null);
                LoaderFunction worker = (LoaderFunction) TestLoader.class.getDeclaredField(workerName).get(this);
                this.workersMap.put(workerName, worker);
            }
        }
    }


    public LoaderFunction getFunction(String taskName) {
        return this.workersMap.get(taskName);
    }

    LoaderFunction<JSONObject, TestInform> testOneName = (input, testDto) -> {
        String name = input.getString("name");
        if(StringUtils.isEmpty(name)){
            return ;
        }
        if(name.equals("llbnk")){
            testDto.setEmail("16*****@qq.com");
        }
    };

    LoaderFunction<JSONObject, TestInform> testTwoName = (input, testDto) -> {
        String name = input.getString("name");
        if(StringUtils.isEmpty(name)){
            return ;
        }
        if(name.equals("llbnk")){
            testDto.setPhone("139*****");
        }
    };



}